package com.handong.handserialprogram.module.guide;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.base.BaseActivity;
import com.handong.handserialprogram.module.others.OtherActivity;
import com.handong.handserialprogram.module.tasklist.TaskListActivity;
import com.handong.handserialprogram.utils.JLog;
import com.handong.handserialprogram.utils.SPUtil;
import com.handong.handserialprogram.utils.SystemUtil;
import com.ninetripods.aopermission.permissionlib.annotation.NeedPermission;
import com.ninetripods.aopermission.permissionlib.annotation.PermissionCanceled;
import com.ninetripods.aopermission.permissionlib.annotation.PermissionDenied;
import com.ninetripods.aopermission.permissionlib.bean.CancelBean;
import com.ninetripods.aopermission.permissionlib.bean.DenyBean;

import cn.pda.serialport.Tools;

public class GuideActivity extends BaseActivity<GuidePresenter> implements GuideView {
    protected static final String TAG = GuideActivity.class.getSimpleName();
    @Override
    protected int getContentViewID() {
        JLog.initialize(true);
        return R.layout.activity_guide;
    }

    @Override
    protected GuidePresenter createPresenter() {
        return new GuidePresenter(this);
    }

    @Override
    protected void initViewByID() {
        // 避免从桌面启动程序后，会重新实例化入口类的activity
     /*   if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                   // return;
                }
            }
        }*/
    }

    @Override
    protected void initViewDataRes() {
        initApps();
    }

    @Override
    public void sendIdSuccess() {
        SPUtil.putIntValue(SPUtil.STOP_MISSION_SET_DISTANCE, 4);
        if (!SPUtil.getBooleanValue(SPUtil.FIRST_BOOT)) {


            SPUtil.putBooleanValue(SPUtil.FIRST_BOOT, true);
//            SPUtil.putStringValue(SPUtil.FOOTRING_SERVER_IP,"132.232.253.210");
//            SPUtil.putStringValue(SPUtil.SERVER_IP, "132.232.253.210");

            SPUtil.putStringValue(SPUtil.FOOTRING_SERVER_IP,"111.230.245.184");
            SPUtil.putStringValue(SPUtil.SERVER_IP, "111.230.245.184");

            SPUtil.putIntValue(SPUtil.MISSION_PORT, 8201);
            SPUtil.putIntValue(SPUtil.UPLOAD_PORT, 8205);
            SPUtil.putIntValue(SPUtil.CHAT_PORT, 8200);
            SPUtil.putIntValue(SPUtil.FILE_PORT, 8202);
            SPUtil.putIntValue(SPUtil.VIDEO_PORT,  8203);
            SPUtil.putIntValue(SPUtil.FOOT_PORT, 8205);

            SPUtil.putBooleanValue(SPUtil.JH_OPEN,true);
            SPUtil.putIntValue(SPUtil.JH_VOICE,60);
            SPUtil.putIntValue(SPUtil.BJ_NUM,1);
            SPUtil.putIntValue(SPUtil.BJ_VOICE,60);
            SPUtil.putIntValue(SPUtil.TS_VOICE,60);
            SPUtil.putIntValue(SPUtil.TS_NUM,1);

            //高精度模式
//            SPUtil.putBooleanValue(SPUtil.OPEN_GD_GPS,true);
//            SPUtil.putStringValue(SPUtil.FOOT_RING_APN_1,"CMIOT");

//            Bundle bundle = new Bundle();
//            bundle.putInt(SPUtil.NAV_INDEX, OtherActivity.INDEX_CONFIG_SERVER);
//            bundle.putBoolean(SPUtil.FIRST_BOOT, true);
//            bundle.putString(SPUtil.NAV_TITLE_NAME, "配置服务器");
//            GuideActivity.this.openActWithAnim(OtherActivity.class, bundle);
//            GuideActivity.this.finish();
//            return;
        }
        Intent intent = new Intent(GuideActivity.this, TaskListActivity.class);
        startActivity(intent);
        GuideActivity.this.overridePendingTransition(R.anim.push_left_in,
                R.anim.push_left_out);
        GuideActivity.this.finish();
    }

    @Override
    public void sendIdFail() {
        mPresenter.errorDialog();
    }

    //MOUNT_UNMOUNT_FILESYSTEMS READ_LOGS  WRITE_EXTERNAL_STORAGE 手持机没有支持该权限 READ_EXTERNAL_STORAGE
    @NeedPermission(value = {
            Manifest.permission.ACCESS_COARSE_LOCATION
            , Manifest.permission.ACCESS_FINE_LOCATION
            , Manifest.permission.ACCESS_WIFI_STATE
            , Manifest.permission.ACCESS_NETWORK_STATE
            , Manifest.permission.CHANGE_WIFI_STATE
            , Manifest.permission.READ_PHONE_STATE
            , Manifest.permission.RECEIVE_BOOT_COMPLETED
            , Manifest.permission.VIBRATE
            ,Manifest.permission.RECORD_AUDIO
            ,Manifest.permission.READ_EXTERNAL_STORAGE
            ,Manifest.permission.WRITE_EXTERNAL_STORAGE
             ,Manifest.permission.CAMERA}, requestCode = 10)
    public void initApps() {
        //Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
        //SDKInitializer.initialize(getApplicationContext());
        SPUtil.remove(SPUtil.MISSION_ID);
        SystemUtil.displayMetricsParams(this);
        SystemUtil.initVoiceLevel(this);
        //获取Imei
        String imeiccr= Tools.getIMEI_CCR(Tools.getIMEI(this)).toUpperCase();
        if (imeiccr.length()!=4){
            imeiccr= Tools.getIMEI_CCR16(Tools.getIMEI(this)).toUpperCase();
        }
        SPUtil.putStringValue(SPUtil.IMEI_CRC,imeiccr);
        JLog.d(TAG,"imei_crc = "+SPUtil.getStringValue(SPUtil.IMEI_CRC));
        JLog.d(TAG,"imei = "+Tools.getIMEI(this));
        //临时修改
        //mPresenter.sendIMEI(imeiccr);
        sendIdSuccess();

    }

    /**
     * 权限被取消
     *
     * @param bean CancelBean
     */
    @PermissionCanceled
    public void dealCancelPermission(CancelBean bean) {
        //Toast.makeText(this, "requestCode:" + bean.getRequestCode(), Toast.LENGTH_SHORT).show();
        mPresenter.cancelDialog();
    }
    /**
     * 权限被拒绝
     *
     * @param bean DenyBean
     */
    @PermissionDenied
    public void dealPermission(DenyBean bean) {
        //Toast.makeText(this,"requestCode:" + bean.getRequestCode()+ ",Permissions: " + Arrays.toString(bean.getDenyList().toArray()), Toast.LENGTH_SHORT).show();
        mPresenter.errorDialog();
    }


    protected void openActWithAnim(Class<? extends BaseActivity> cls, Bundle paramBundle) {
        Intent intent = new Intent(this, cls);
        if (paramBundle != null) {
            intent.putExtras(paramBundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in,
                R.anim.push_left_out);
    }

}
